<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="profile.photo"
            @click="$refs.iptFile.click()"
          />
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            accept="image/*"
            @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell title="名称" is-link :value="profile.name" @click="showNameDialogFn" />
      <van-dialog v-model="isShowNameDialog"
       title="修改名称"
       show-cancel-button
       :before-close="onNameDialogBeforeClose">
        <van-field
        v-model.trim="userName"
        input-align="center"
        maxlength="7"
        placeholder="请输入名称"
        v-fofo />
      </van-dialog>
      <van-cell title="生日" is-link :value="profile.birthday" @click="showBirthFn" />
      <van-popup v-model="isShowBirth" position="bottom" style="height: 50%" round>
      <!-- 日期选择控件 -->
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择出生日期"
        :min-date="minDate"
        :max-date="maxDate"
        :show-toolbar="true"
        @cancel="isShowBirth = false"
        @confirm="confirmFn"
      />
</van-popup>
    </van-cell-group>
  </div>
</template>

<script>
import { userProfileAPI, updatePhotoAPI, updateProfileAPI } from '@/api'
import { Notify } from 'vant'
import dayjs from 'dayjs'
export default {
  data () {
    return {
      profile: {},
      isShowNameDialog: false,
      userName: '',
      isShowBirth: false,
      currentDate: new Date(),
      minDate: new Date(1990, 0, 1),
      maxDate: new Date()
    }
  },
  name: 'UserEdit',
  methods: {
    async onFileChange (ev) {
      // console.log(ev.target.files[0])
      if (ev.target.files.length === 0) return
      const fd = new FormData()
      fd.append('photo', ev.target.files[0])
      const res = await updatePhotoAPI(fd)
      this.profile.photo = res.data.data.photo // 更新最新头像
    },
    async onNameDialogBeforeClose (action, done) {
      if (action === 'confirm') {
        // 确定
        // unicode编码 \u4092
        // url编码 %E2%C3%D1
        if (/^[A-Za-z0-9\u4e00-\u9fa5]{1,7}$/.test(this.userName)) {
          // 通过校验
          // 调用接口
          // 更新页面显示的名字

          // 关闭弹窗
          await updateProfileAPI({ userName: this.userName })
          this.profile.name = this.userName
          done()
        } else {
          // 提示用户
          Notify({ type: 'warning', message: '用户名中英文和数字1-7位' })
          // 阻止弹窗关闭
          done(false)
        }
      } else if (action === 'cancel') {
        // 取消
        done()
      }
    },
    showNameDialogFn () {
      this.isShowNameDialog = true
      this.userName = this.profile.name // 设置默认显示内容
    },
    showBirthFn () {
      this.isShowBirth = true
      this.currentDate = new Date(this.profile.birthday)
    },
    async confirmFn () {
      const dateStr = dayjs(this.currentDate).format('YYYY-MM-DD')
      await updateProfileAPI({
        birthday: dateStr
      })
      // 前端页面同步
      this.profile.birthday = dateStr
      // 时间选择器关闭
      this.isShowBirth = false
    }
  },
  async created () {
    const res = await userProfileAPI()
    console.log(res)
    this.profile = res.data.data
  }

}
</script>

<style lang="less" scoped>
.user-edit-container {
  text-align: left;
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
</style>
